package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private LogService logService;

    @Override
    public Map<String, Object> getSupplierList(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map=new HashMap<>();
        int total=supplierDao.getSupplierTotalCount(supplierName);

        int start=(page-1)*rows;

        System.out.println(start+"22222222222");
        List<Supplier> list=supplierDao.getRows(start,rows,supplierName);
        map.put("total",total);
        map.put("rows",list);
        System.out.println(list.get(0)+"11111111111111");
        return map;
    }

    @Override
    public void save(Supplier supplier) {
        // ID为空时，说明是新增操作，需要先判断用户名是否存在
        if(supplier.getSupplierId() == null){
            supplierDao.addSupplier(supplier);
            logService.save(new Log(Log.INSERT_ACTION,"添加供应商:"+supplier.getSupplierName()));
        } else {
            supplierDao.updateSupplier(supplier);
            logService.save(new Log(Log.UPDATE_ACTION,"修改供应商:"+supplier.getSupplierName()));
        }
    }

    @Override
    public ServiceVO delete(String ids) {
        String[] idArray = ids.split(",");//分割字符串为id数组，循环遍历删除或xml里foreach标签

        for (String id : idArray) {
            supplierDao.delete(id);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
